# 主要用于保存token
from redis import StrictRedis
import redis
import uuid
import time


class MyRedis(object):
    def __init__(self, host='120.24.90.180', port=6379):
        self.__redis = redis.Redis(host=host, port=port)
        self.__timeout = 30 * 60

    def store_token(self, token, user_right):
        """
        将token与对应的权限存入数据库
        """
        self.__redis.setex(token, user_right, self.__timeout)

    def check_token(self, token):
        """
        检查数据库中是否存在token, 并返回对应的权限，重置失效时间
        """
        flag = self.__redis.exists(token)
        if flag:
            self.__redis.expire(token, self.__timeout)
            right = self.__redis.get(token)
            return flag, right
        else:
            return flag, 0
